package com.szh.mysobproject.ui.activity

import android.app.Activity
import android.content.Intent
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.hjq.bar.TitleBar
import com.szh.mysobproject.R
import com.szh.mysobproject.action.StatusAction
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.databinding.FishPondSelectionActivityBinding
import com.szh.mysobproject.other.IntentKey
import com.szh.mysobproject.ui.adapter.FishPondSelectionAdapter
import com.szh.mysobproject.util.toJson
import com.szh.mysobproject.viewmodel.fishpond.FishPondViewModel
import com.szh.mysobproject.widget.StatusLayout

/**
 * @author : create by  szh
 * @date : 2022/7/18 10:47
 * @desc :标签列表选择页面
 */

class FishPondSelectionActivity : AppActivity(), StatusAction {

    private val mBinding by viewBinding<FishPondSelectionActivityBinding>()
    private val mFishPondViewModel by viewModels<FishPondViewModel>()
    private val mFishPondSelectionAdapter = FishPondSelectionAdapter()


    override fun getLayoutId(): Int = R.layout.fish_pond_selection_activity

    override fun initView() {
        mBinding.rvFishPondLabelsList.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mFishPondSelectionAdapter
        }
    }

    override fun initData() {
        showLoading()
        val refreshLayout = mBinding.refreshLayout
        mFishPondViewModel.loadTopicList().observe(this) {
            refreshLayout.finishRefresh()
            val data = it.getOrElse {
                showError {
                    initData()
                }
                return@observe
            }
            if (data.isEmpty()) {
                showEmpty()
            } else {
                showComplete()
            }
            mFishPondSelectionAdapter.setData(data)
        }
    }

    override fun initEvent() {
        mBinding.refreshLayout.setOnRefreshListener {
            initData()
        }
        mFishPondSelectionAdapter.setOnItemClickListener { item, _ ->
            val data = Intent().apply {
                putExtra(IntentKey.OTHER, item.toJson())
            }
            setResult(Activity.RESULT_OK, data)
            finish()
        }
    }

    override fun onRightClick(titleBar: TitleBar?) {
        setResult(Activity.RESULT_OK,null)
        finish()
    }

    override fun getStatusLayout(): StatusLayout = mBinding.slFishPondSelectionLayout
}